package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduCourseDescription;
import com.atguigu.eduservice.entity.frontVo.CourseFrontVo;
import com.atguigu.eduservice.entity.frontVo.CourseWebVo;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.mapper.EduCourseMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseDescriptionService;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-04-12
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired  //课程描述注入
    private EduCourseDescriptionService eduCourseDescriptionService;    // 将EduCourseDescriptionService注入

    @Autowired  //课程小节注入
    private EduVideoService eduVideoService;

    @Autowired  //章节注入
    private EduChapterService eduChapterService;

    //添加课程基本信息
    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        //向课程表添加信息
        //将courseInfoVo 转换为EduCourse里的类
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int insert = baseMapper.insert(eduCourse);  // 返回影响行数

        if(insert==0){
            try {
                throw new Exception("添加课程失败");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        String cid = eduCourse.getId();
        //向课程描述表添加信息
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        //设置课程描述对应的课程的id
        eduCourseDescription.setId(cid);
        eduCourseDescriptionService.save(eduCourseDescription);
        return cid;
    }

    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        //先根据课程id查询课程表
        EduCourse course = baseMapper.selectById(courseId);
        //根据课程id查询课程描述表
        EduCourseDescription description = eduCourseDescriptionService.getById(courseId);
        //将数据封装到courseInfoVo
        BeanUtils.copyProperties(course, courseInfoVo);
        BeanUtils.copyProperties(description, courseInfoVo);

        return courseInfoVo;
    }

    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        //1  先修改课程表
        EduCourse course = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, course);
        int update = baseMapper.updateById(course);
        if(update==0){
            try {
                throw new Exception("修改课程信息失败");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //2  修改课程描述表
        EduCourseDescription description = new EduCourseDescription();
        BeanUtils.copyProperties(courseInfoVo,description);
        boolean update1 = eduCourseDescriptionService.updateById(description);

    }

    //根据课程id 查询课程确认信息
    @Override
    public CoursePublishVo publishCourseInfo(String courseId) {
        //调用mapper
        CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(courseId);
        return publishCourseInfo;
    }

    //删除课程
    @Override
    public void removeCourse(String courseId) {
        //根据课程Id删除小节
        eduVideoService.removeByCourseId(courseId);
        //删除章节
        eduChapterService.removeByCourseId(courseId);
        //删除课程描述
        eduCourseDescriptionService.removeById(courseId);
        //删除课程本身
        int delete = baseMapper.deleteById(courseId);
        if(delete == 0){
            try {
                throw new Exception("删除课程失败");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    //条件查询带分页--课程
    @Override
    public Map<String, Object> getCourseFrontList(Page<EduCourse> p, CourseFrontVo courseFrontVo) {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        //判断条件
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())){
            //判断一级分类是否存在
            wrapper.eq("subject_parent_id",courseFrontVo.getSubjectParentId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectId())){
            //判断二级分类是否存在
            wrapper.eq("subject_id",courseFrontVo.getSubjectId());
        }
        if (!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())) {
            wrapper.orderByDesc("buy_count");
        }

        if (!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())) {
            wrapper.orderByDesc("gmt_create");
        }

        if (!StringUtils.isEmpty(courseFrontVo.getPriceSort())) {
            wrapper.orderByDesc("price");
        }

        baseMapper.selectPage(p,wrapper);
        List<EduCourse> records = p.getRecords();
        long current = p.getCurrent();
        long pages = p.getPages();
        long size = p.getSize();
        long total = p.getTotal();
        boolean hasNext = p.hasNext();
        boolean hasPrevious = p.hasPrevious();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;
    }

    @Override
    public CourseWebVo getBaseInfo(String courseId) {
        return baseMapper.getBaseInfo(courseId);
    }
}
